package com.starwish.starwishblog.web.back;

import com.github.pagehelper.PageInfo;
import com.starwish.starwishblog.result.ResultResponse;
import com.starwish.starwishblog.service.ArticleService;
import com.starwish.starwishblog.vo.ArticleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;


@RestController
@RequestMapping("admin/article")
public class ArticleController {

    @Autowired
    ArticleService articleService;

//    @GetMapping("/add")
//    public ResultResponse<Void> issueArticle(@RequestParam("html")String html){
//        System.out.println(html);
//        return null;
//    }

//    @GetMapping("/getbody")
//    public ResultResponse<String> getArticle(){
//        return new ResultResponse<String>(200,"成功","<p>模拟&nbsp;Ajax&nbsp;后台数据&nbsp;HTML</p>");
//    }

    @GetMapping("/articleList")
    public ResultResponse<PageInfo<ArticleVo>> articleList(@RequestParam(name="now",defaultValue = "1")Integer now,
                                                                 @RequestParam(name="size",defaultValue = "5")Integer size,
                                                                 String aname,String uname){
        System.out.println(aname + uname);
        ResultResponse<PageInfo<ArticleVo>> result = null;
        try {
            PageInfo<ArticleVo> articlePage = articleService.findArticleByCons(now,size,aname,uname);
            result = new ResultResponse<>(200,"查询文章列表成功",articlePage);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(205,"查询文章列表失败");
        }
        return result;
    }

    @GetMapping("/getArticlesByCons")
    public ResultResponse<PageInfo<ArticleVo>> articleList(@RequestParam(name="now",defaultValue = "1")Integer now,
                                                           @RequestParam(name="size",defaultValue = "5")Integer size,
                                                           String aname,String uname,Integer status){
        System.out.println(aname + uname);
        ResultResponse<PageInfo<ArticleVo>> result = null;
        try {
            PageInfo<ArticleVo> articlePage = articleService.findArticleByConsAndStatus(now,size,aname,uname,status);
            result = new ResultResponse<>(200,"查询文章列表成功",articlePage);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(205,"查询文章列表失败");
        }
        return result;
    }

    @PostMapping("/changeStatus")
    public ResultResponse<Void> changeStatus(@RequestParam("articleId")Integer articleId,
                                                           @RequestParam("status")Integer status,
                                                           String result){
        ResultResponse<Void> result1 = null;
        try {
            int i = articleService.changeStatus(articleId,status,result);
            result1 = new ResultResponse<>(200,"审核文章成功");
        } catch (Exception e) {
            e.printStackTrace();
            result1 = new ResultResponse<>(205,"审核文章失败");
        }
        return result1;
    }

    @PutMapping("/modifyArticle")
    public ResultResponse<Void> modifyArticle(@RequestBody ArticleVo articleVo){
        ResultResponse<Void> result = null;
        try {
            int i = articleService.modifyArticle(articleVo);
            result = new ResultResponse<>(200,"修改文章成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"系统异常");
        }
        return result;
    }

    @GetMapping("/modifyContent")
    public ResultResponse<Void> modifyContent(@RequestParam("bid")Integer bid,@RequestParam("html")String html){
        System.out.println(html);
        ResultResponse<Void> result = null;
        try {
            int i = articleService.modifyContent(bid, HtmlUtils.htmlEscape(html));
            if(i > 0){
                result = new ResultResponse<>(200,"修改成功");
            }else{
                result = new ResultResponse<>(201,"修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(305,"修改异常");
        }
        return result;
    }

    @GetMapping("/changeWeight")
    public ResultResponse<Void> changeWeight(@RequestParam("id")Integer id,@RequestParam("weight") Integer weight){
        ResultResponse<Void> result = null;
        try {
            int i = articleService.modifyWeight(id,weight);
            if(i > 0){
                result = new ResultResponse<>(200,"修改成功");
            }else{
                result = new ResultResponse<>(201,"修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(205,"系统错误");
        }
        return result;
    }


    @DeleteMapping("/del/{id}")
    public ResultResponse<Void> delById(@PathVariable(name="id")Integer id){
        ResultResponse<Void> result = null;
        try {
            int i = articleService.removeByKey(id);
            if(i > 0){
                result = new ResultResponse<>(200,"删除成功");
            }else{
                result = new ResultResponse<>(201,"删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(205,"系统错误");
        }
        return result;
    }
}
